#include "groupsessiondetailwidget.h"
#include"sessiondetailwidget.h"
#include"debug.h"
#include"./model/data.h"
#include"./model/datacenter.h"

#include<QVBoxLayout>
#include<QScrollArea>
#include<QScrollBar>
#include<QPushButton>
#include<QList>
#include<QLineEdit>

GroupSessionDetailWidget::GroupSessionDetailWidget(QWidget *parent)
    :QDialog(parent)
{
    //1.设置窗口的基本属性
    this->setFixedSize(410,600);
    this->setWindowTitle("群组详情");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QDialog { background-color: rgb(255,255,255);}");
    this->setAttribute(Qt::WA_DeleteOnClose);

    //2.创建布局管理器
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(10);
    vlayout->setContentsMargins(50,20,50,50);
    vlayout->setAlignment(Qt::AlignTop);
    this->setLayout(vlayout);

    //3.创建滚动区域
    //3.1创建 QScrollArea 对象
    QScrollArea* scrollArea = new QScrollArea();
    scrollArea->setWidgetResizable(true);
    scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 5px; background-color: rgb(255,255,255);}");
    scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height: 0;}");
    scrollArea->setStyleSheet("QWidget { background-color: transparent;border: none;}");
    scrollArea->setFixedSize(310,350);
    scrollArea->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    //3.2创建一个QscrollArea 内部的QWidget
    QWidget* container = new QWidget();
    scrollArea->setWidget(container);

    //3.给container 里面添加一个网络布局
    glayout = new QGridLayout();
    glayout->setSpacing(10);
    glayout->setContentsMargins(0,0,0,0);
    glayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    container->setLayout(glayout);

    //3.4把滚动区域添加到布局管理器中
    vlayout->addWidget(scrollArea);

    //4.添加"添加按钮"
    addBtn = new AvatarItem(QIcon(":/resource/image/cross.png"),"添加");
    glayout->addWidget(addBtn,0,0);

    //5.添加"群聊名称"
    groupNameTag = new QLabel();
    groupNameTag->setText("群聊名称");
    groupNameTag->setFixedHeight(50);
    groupNameTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding);
    groupNameTag->setStyleSheet("QLabel { font-weight: 700; font-size: 16px}");
    //设置文字在QLabel内部的对齐方式
    groupNameTag->setAlignment(Qt::AlignBottom);
    //设置QLabel在布局管理器中的位置
    vlayout->addWidget(groupNameTag);

    //6.添加真实的群聊名字和修改按钮
    //6.1创建水平布局
    hlayout = new QHBoxLayout();
    hlayout->setSpacing(0);
    vlayout->addLayout(hlayout);

    //6.2 创建真实群聊名字的QLabel
    groupNameLabel = new QLabel();
    groupNameLabel->setFixedHeight(50);
    groupNameLabel->setStyleSheet("QLabel { font-size: 18px;}");
    groupNameLabel->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding);
    hlayout->addWidget(groupNameLabel,0,Qt::AlignLeft | Qt::AlignVCenter);

    //6.3创建"修改按钮"
    groupNameModifyBtn = new QPushButton();
    groupNameModifyBtn->setFixedSize(30,30);
    groupNameModifyBtn->setIconSize(QSize(20,20));
    groupNameModifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    groupNameModifyBtn->setStyleSheet("QPushButton { border: none; background-color: transparent; } QPushButton:pressed {background-color: rgb(230,230,230);}");
    hlayout->addWidget(groupNameModifyBtn,0,Qt::AlignRight | Qt::AlignRight);

    //6.4创建"群聊名称"编辑框
    groupNameEdit = new QLineEdit();
    groupNameEdit->setFixedHeight(50);
    groupNameEdit->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding);
    groupNameEdit->setObjectName("groupNameEdit");
    groupNameEdit->setStyleSheet("#groupNameEdit { font-size:18px; border: 1px solid gray; border-left:none;border-right:none;}");
    groupNameEdit->hide();

    //6.5创建"提交按钮"
    groupNameSubmitBtn = new QPushButton();
    groupNameSubmitBtn->setFixedSize(30,30);
    groupNameSubmitBtn->setIconSize(QSize(20,20));
    groupNameSubmitBtn->setIcon(QIcon(":/resource/image/submit.png"));
    groupNameSubmitBtn->setStyleSheet("QPushButton { border: none; background-color: transparent; } QPushButton:pressed {background-color: rgb(230,230,230);}");
    groupNameSubmitBtn->hide();

    //7.退出群聊按钮
    exitGroupBtn = new QPushButton();
    exitGroupBtn->setText("退出群聊");
    exitGroupBtn->setFixedHeight(50);
    exitGroupBtn->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    QString btnStyle = "QPushButton { border: 1px solid rgb(90,90,90); border-radius: 5px;background-color: transparent;}";
    btnStyle += "QPushButton:pressed {background-color: rgb(230,230,230);}";
    exitGroupBtn->setStyleSheet(btnStyle);
    vlayout->addWidget(exitGroupBtn);


    //此处构造假的数据用来测试界面
#if TEST_UI
    groupNameLabel->setText("人类吃喝研究小组");
    QIcon avatar(":/resource/image/defaultAvatar.png");
    for(int i = 0;i < 50;++i){
        AvatarItem* item = new AvatarItem(avatar,"张三" + QString::number(i));
        this->addMember(item);
    }

#endif

    //8.从服务器加载数据
    initData();

    //9.初始化槽函数
    initSingalSlot();
}

void GroupSessionDetailWidget::addMember(AvatarItem *avatarItem)
{
    const int MAX_Col = 4;
    if(curCol >= MAX_Col){
        //换行操作
        ++curRow;
        curCol = 0;
    }
    //拿到网格管理器直接添加
    glayout->addWidget(avatarItem,curRow,curCol);
    ++curCol;
}

void GroupSessionDetailWidget::initData()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(dataCenter,&model::DataCenter::getMemberListDone,this,&GroupSessionDetailWidget::initMembers);
    dataCenter->getMemberListAsync(dataCenter->getCurrentChatSessionId());
}

void GroupSessionDetailWidget::initMembers(const QString& chatSessionId)
{
    //根据刚才拿到的成员列表，把成员列表渲染到界面上
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    QList<model::UserInfo>*  memberList = dataCenter->getMemberList(chatSessionId);
    if(memberList == nullptr){
        LOG() << "获取的成员列表为空!chatSessionId" << chatSessionId;
        return;
    }
    //遍历成员列表
    for(const auto& u : *memberList){
        AvatarItem* avatarItem = new AvatarItem(u.avatar,u.nickname);
        this->addMember(avatarItem);
    }

    //群聊名称，此处先设为固定值
    groupNameLabel->setText("新的群聊");

}

void GroupSessionDetailWidget::initSingalSlot()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(groupNameModifyBtn,&QPushButton::clicked,this,[=](){
        //把当前的groupNameLabel和groupNameModifyBtn隐藏起来
        groupNameLabel->hide();
        groupNameModifyBtn->hide();
        hlayout->removeWidget(groupNameLabel);
        hlayout->removeWidget(groupNameModifyBtn);
        //把groupNameEdit和groupNameSubmitBtn显示出来
        groupNameEdit->show();
        groupNameSubmitBtn->show();
        hlayout->addWidget(groupNameEdit,0,Qt::AlignLeft | Qt::AlignVCenter);
        hlayout->addWidget(groupNameSubmitBtn,0,Qt::AlignRight | Qt::AlignVCenter);
        //把输入框的内容进行设置
        groupNameEdit->setText(groupNameLabel->text());
    });

    connect(groupNameSubmitBtn,&QPushButton::clicked,this,&GroupSessionDetailWidget::clickgroupNameSubmitBtn);

    //2.退出群聊
    //3.群聊添加新成员
}

void GroupSessionDetailWidget::clickgroupNameSubmitBtn()
{
    //1.从输入框中，拿到修改后的昵称
    const QString& groupName = groupNameEdit->text();
    if(groupName.isEmpty()){
        return ;
    }

    //2.发送网络请求
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(dataCenter,&model::DataCenter::changeGroupNameDone,this,&GroupSessionDetailWidget::clickgroupNameSubmitBtnDone,Qt::UniqueConnection);
    dataCenter->changeGroupNameAsync(groupName);
}

void GroupSessionDetailWidget::clickgroupNameSubmitBtnDone()
{
    //对界面空间进行切换，把刚才输入框切换成label,把提交按钮切换成编辑按钮
    //同时还需要把输入框中的本文设置成label中的文本
    hlayout->removeWidget(groupNameEdit);
    groupNameEdit->hide();
    hlayout->addWidget(groupNameLabel,0,Qt::AlignLeft | Qt::AlignVCenter);
    groupNameLabel->show();
    groupNameLabel->setText(groupNameEdit->text());

    hlayout->removeWidget(groupNameSubmitBtn);
    groupNameSubmitBtn->hide();
    hlayout->addWidget(groupNameModifyBtn,0,Qt::AlignRight | Qt::AlignVCenter);
    groupNameModifyBtn->show();
}

void GroupSessionDetailWidget::clickexitGroupBtn()
{

}

void GroupSessionDetailWidget::clickexitGroupBtnDone()
{

}


